﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spore.Validator;
using System.IO;
using DevExpress.XtraEditors;
using QunYing.Common;
using System.ServiceModel;


namespace Client
{
    public partial class VipAddModify : BasePopForm
    {
       List<QunYing.Model.VipServices> services;
       List<QunYing.Model.VipRank> vipRank;
       QunYing.BLL.VipInfo bll_vipInfo = new QunYing.BLL.VipInfo();
       QunYing.Model.VipInfo m_vipInfo;
       public VipAddModify()
        {
            InitializeComponent();
            this.NecessaryRemark(this.txt_会员ID, this.txt_经销商,
                this.txt_通信地址, this.txt_移动电话, this.txt_固定电话, this.txt_服务项,
                this.txt_VipCardID, this.txt_name, this.txt_省份, this.txt_城市,this.dte_到期时间,
                this.dte_入会时间);
            this.cbe_会员级别.SelectedIndexChanged += new EventHandler(cbe_会员级别_SelectedIndexChanged);
            this.txt_经销商.Text = GlobalInfo.UserLoginInfo.用户ID;
            this.dte_入会时间.DateTime = DateTime.Now;
            this.FormValidator.SetControlValitors(txt_会员ID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(txt_省份, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.cbe_会员级别, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_VipCardID, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_固定电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_移动电话, ValidatorDefaults.Phone);
            this.FormValidator.SetControlValitors(this.txt_通信地址, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_城市, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_email, ValidatorDefaults.Email);
            this.FormValidator.SetControlValitors(this.txt_name, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.dte_入会时间, ValidatorDefaults.NotNull);
            this.FormValidator.SetControlValitors(this.txt_缴费金额, ValidatorDefaults.Integer);
            this.FormValidator.SetControlValitors(this.dte_到期时间, ValidatorDefaults.NotNull);
            this.services = (new QunYing.BLL.VipServices()).GetModelList(string.Empty);
            this.vipRank = (new QunYing.BLL.VipRank()).GetModelList(string.Empty);
            this.cbx_重新审核.ForeColor = Color.FromArgb(24, 66, 138);
            this.cbx_重新审核.Checked = true;
        }

        void cbe_会员级别_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_服务项.BeginUpdate();
            this.txt_服务项.Items.Clear();
            string[] strs = vipRank.First((v) => { return v.等级名称 == cbe_会员级别.Text; }).基本服务条款.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
             foreach(var v in strs)
             {
                 var temp = services.First((vv) => { return vv.ID.ToString() == v; });
                 this.txt_服务项.Items.Add(temp, false);
             }
             if (m_vipInfo != null)
             {
                 if(m_vipInfo.所选服务项!=null)
                 {
                 string[] strss = this.m_vipInfo.所选服务项.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                 foreach (var vv in strss)
                 {
                     QunYing.Model.VipServices vs = this.services.FirstOrDefault((ee) => ee.ID.ToString() == vv);
                     this.txt_服务项.SetItemChecked(this.txt_服务项.Items.IndexOf(vs), true);
                 }
                 }
             }
            this.txt_服务项.EndUpdate();
        }

        protected override void OnLoad(EventArgs e)
        {
            
            base.OnLoad(e);
        }

        public void BindData(QunYing.Model.VipInfo vInfo)
        {
            this.m_vipInfo = vInfo;
            var opera = ClientProxyFactory.GetProxy<IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
            Bitmap[] bp = opera.GetImages(vInfo.经销商编号, new string[] { m_vipInfo.会员卡号 + ".jpg" });
            if (bp.Length != 0)
            {
                this.pet_地址图片.Image = bp[0];
            }
            (opera as ICommunicationObject).Close();
            this.txt_email.Text = m_vipInfo.电邮地址;
            this.txt_name.Text = m_vipInfo.用户姓名;
            this.txt_VipCardID.Text = m_vipInfo.会员卡号;
            this.txt_城市.Text = m_vipInfo.所在城市;
            this.txt_固定电话.Text = m_vipInfo.固定电话;
            this.txt_会员ID.Text = m_vipInfo.用户ID;
            this.txt_通信地址.Text = m_vipInfo.通信地址;
            this.txt_移动电话.Text = m_vipInfo.移动电话;
            this.txt_省份.Text = m_vipInfo.所在省份;
            this.cbe_会员级别.SelectedIndex = this.cbe_会员级别.Properties.Items.IndexOf(m_vipInfo.会员级别);
            this.dte_到期时间.DateTime = m_vipInfo.到期时间;
            this.dte_入会时间.DateTime = m_vipInfo.入会时间;
            this.txt_其他信息.Text = m_vipInfo.其他信息;
            if (m_vipInfo.所选服务项 == null) return;
            string[] strs = this.m_vipInfo.所选服务项.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (var vv in strs)
            {
                QunYing.Model.VipServices vs = this.services.FirstOrDefault((ee) => ee.ID.ToString() == vv);
                this.txt_服务项.SetItemChecked(this.txt_服务项.Items.IndexOf(vs), true);
            }
            this.txt_缴费金额.Text = m_vipInfo.首次缴费.ToString();
            
           
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
            
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (!this.FormValidator.ValidAll()) return;
            try
            {
                if (!string.IsNullOrEmpty(this.txt_地址图片.Text))
                {
                    if (!File.Exists(txt_地址图片.Text))
                    {
                        this.ep.SetError(this.txt_地址图片, "图片文件不存在，如不修改请把图片地址栏清空！");
                        return;
                    }
                    try
                    {
                        QunYing.Common.IImageOperator opera = QunYing.Common.ClientProxyFactory.GetProxy<QunYing.Common.IImageOperator>(QunYing.Common.GlobalInfo.ServiceImageOperationVip);
                        opera.UpLoad(m_vipInfo.经销商编号, new string[] { this.txt_VipCardID.Text + ".jpg" }, new Bitmap[] { (Bitmap)Bitmap.FromFile(txt_地址图片.Text) });
                        (opera as ICommunicationObject).Close();
                    }
                    catch (Exception ex)
                    {
                        QunYing.Common.Loger.Log.Error("上传图片时发生错误:" + ex.Message);
                    }
                }
              
            
                //QunYing.Model.VipInfo vipInfo = new QunYing.Model.VipInfo();
                m_vipInfo.到期时间 = this.dte_到期时间.DateTime;
                m_vipInfo.入会时间 = this.dte_入会时间.DateTime;
                m_vipInfo.电邮地址 = this.txt_email.Text.Trim();
                m_vipInfo.固定电话 = this.txt_固定电话.Text.Trim();
                m_vipInfo.会员级别 = this.cbe_会员级别.Text;
                m_vipInfo.会员卡号 = this.txt_VipCardID.Text;
                m_vipInfo.经销商编号 = this.txt_经销商.Text;
                m_vipInfo.移动电话 = this.txt_移动电话.Text;
              
                m_vipInfo.其他信息 = this.txt_其他信息.Text;
                string services = string.Empty;
                foreach (var vv in this.txt_服务项.CheckedItems)
                {
                    services += this.services.FirstOrDefault((ee) => ee.服务项 == vv.ToString()).ID.ToString() + ",";
                }
                services = services.TrimEnd(',');
                m_vipInfo.所选服务项 = services;
                m_vipInfo.所在城市 = this.txt_城市.Text;
                m_vipInfo.所在省份 = this.txt_省份.Text;
                m_vipInfo.通信地址 = this.txt_通信地址.Text;
                m_vipInfo.移动电话 = this.txt_移动电话.Text;
                m_vipInfo.用户ID = this.txt_会员ID.Text;
                m_vipInfo.用户姓名 = this.txt_name.Text;
                m_vipInfo.首次缴费 = Int32.Parse(this.txt_缴费金额.Text.Trim());
                if (this.cbx_重新审核.Checked)
                {
                    m_vipInfo.审核状态 = "未审核";
                }
                this.bll_vipInfo.Update(m_vipInfo);
                GlobalInfo.MsgBoxShow("会员资料已经修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.DialogResult = DialogResult.OK;
            
            }
            catch (Exception ex)
            {
                GlobalInfo.MsgBoxShowError("程序出现错误：" + ex.Message);
            }
        }

        private void btn_图片_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.CheckPathExists = true;
            ofd.Multiselect = false;
            ofd.Filter = string.Format("{0}|{1};{2};{3}", "图片文件", "*.jpg", "*.pgn", "*.bmp");
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                this.txt_地址图片.Text = ofd.FileName;
                this.pet_地址图片.Image = Bitmap.FromFile(txt_地址图片.Text);
            }
        }
    }
}
